/**
 * 所有版权归 广西梧州 陈锦韬 所有
 *
 * @Title: NetShareInfo
 * @Package com.rx.stock.vo
 * @Description: 股票信息网络
 * @author: 陈锦韬
 * @date: 2021\9\9 0009
 * @version V1.0
 * @Copyright: 2021 陈锦韬  All rights reserved.
 */
package com.rx.stock.vo;

import com.rx.core.base.RxBaseConstant;
import com.rx.core.iface.RxContent;
import com.rx.core.util.EmptyChecker;
import com.rx.core.util.RxUtil;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author: Administrator
 * @Description: 股票信息网络
 * @date: 2021\9\9 0009
 */
@Data
public class NetLatestShareInfo implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 时间
     */
    private String sj;

    /**
     * 开盘价
     */
    private String kpj;
    /**
     * 收盘价
     */
    private String xj;
    /**
     * 最高价
     */
    private String zg;
    /**
     * 最低价
     */
    private String zd;
    /**
     * 换手率
     */
    private String hsl;
    /**\
     * 成交量
     */
    private String cjl;
    /**
     * 成交额
     */
    private String cje;

    private String zdbfb;


    /**
     * 开盘价
     */
    private static BigDecimal openPrice;


    /**
     * 收盘价
     */
    private static BigDecimal closePrice;

    /**
     * 最高价
     */
    private static BigDecimal highestPrice;

    /**
     * 最低价
     */
    private static BigDecimal lowestPrice;

    /**
     * 换手率
     */
    private static BigDecimal switchRate;

    /**
     * 成交量
     */
    private static BigDecimal turnOver;

    /**
     * 成交额
     */
    private static BigDecimal turnAmount;

    /**
     * 涨跌
     */
    private static BigDecimal changeRate;

    private static String removeComma(String amount){
        if (EmptyChecker.isEmpty(amount)){
            return "0";
        }
        return amount.replace(",","");
    }

    public static NetLatestShareInfo createStockShareInfo(String kline){
        String[] fields = kline.split(",");
        NetLatestShareInfo netHiShareInfo = new NetLatestShareInfo();
        // 成交额
        BigDecimal tmp = new BigDecimal(removeComma(fields[6]));
        netHiShareInfo.setCje(RxUtil.Amount.divideRound(tmp,10000,6).toPlainString());
        // 成交量 手换成股
        netHiShareInfo.setCjl(removeComma(fields[5]));
        // 涨跌幅
        netHiShareInfo.setZdbfb(removeComma(fields[8]));
        // 换手率
        netHiShareInfo.setHsl(removeComma(fields[10]));
        // 最低价
        netHiShareInfo.setZd(removeComma(fields[4]));
        // 最高价
        netHiShareInfo.setZg(removeComma(fields[3]));
        // 收盘价
        netHiShareInfo.setXj(removeComma(fields[2]));
        // 开盘价
        netHiShareInfo.setKpj(removeComma(fields[1]));
        // 时间
        netHiShareInfo.setSj(fields[0]);

        return netHiShareInfo;
    }
    public  StockHistoryInfo createStockShareInfo(int flg){
        StockHistoryInfo stockShareInfo = new StockHistoryInfo();

        stockShareInfo.setLowestPrice(RxUtil.Amount.createWithDefault(zd,lowestPrice));
        stockShareInfo.setOpenPrice(RxUtil.Amount.createWithDefault(kpj,openPrice));
        stockShareInfo.setSwitchRate(RxUtil.Amount.createWithDefault(hsl,switchRate));

        stockShareInfo.setChangeRate(RxUtil.Amount.createWithDefault(zdbfb,changeRate));
        stockShareInfo.setClosePrice(RxUtil.Amount.createWithDefault(xj,closePrice));
        stockShareInfo.setHighestPrice(RxUtil.Amount.createWithDefault(zg,highestPrice));

        stockShareInfo.setTurnAmount(RxUtil.Amount.createWithDefault(cje,turnAmount,10000));
        stockShareInfo.setTurnOver(RxUtil.Amount.createWithDefault(cjl,turnOver,100));

        changeRate = stockShareInfo.getChangeRate();
        closePrice = stockShareInfo.getClosePrice();
        highestPrice = stockShareInfo.getHighestPrice();

        turnAmount = stockShareInfo.getTurnAmount();
        turnOver = stockShareInfo.getTurnOver();

        lowestPrice = stockShareInfo.getLowestPrice();
        openPrice = stockShareInfo.getOpenPrice();
        switchRate = stockShareInfo.getSwitchRate();

        String format = "yyyyMMddHHmmss";
        String suffix = "120000";
        if (flg == 1){

            format = RxBaseConstant.DATE_FORMAT_DATETIME;
            suffix = " 12:00:00";
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date date = null;
        try {
            if (flg == 1){
                date = simpleDateFormat.parse(sj + suffix);
            }else {
                date = simpleDateFormat.parse(sj.substring(0,8) + suffix);
            }

        } catch (ParseException e) {
            date = new Date();
        }
        stockShareInfo.setTradeTime(date);
        return stockShareInfo;
    }
}
